﻿package emusic.api 
{
	import emusic.preview.AlbumPreviewer;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import newcommerce.data.MediaData;
	import newcommerce.media.MP3Player;
	import emusic.api.clients.AlbumClient;
	import emusic.api.clients.AlbumSearchRequest;
	import emusic.api.clients.ArtistClient;
	import emusic.api.clients.ArtistSearchRequest;
	import emusic.api.clients.M3UClient;
	import emusic.api.data.AlbumData;
	import emusic.api.data.SampleTrackData;
	import emusic.api.data.SummaryData;
	import emusic.api.events.AlbumSearchEvent;
	import emusic.api.events.ArtistSearchEvent;
	import emusic.api.events.M3UEvent;
	import emusic.api.feeds.SampleTrackFeed;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Martin Legris ( http://blog.martinlegris.com )
	 */
	public class EMusicTest extends Sprite
	{
		protected var _albumWs:AlbumClient;
		protected var _artistWs:ArtistClient;
		protected var _eMusic:EMusicClient;
		protected var _m3uClient:M3UClient;
		protected var _mp3Player:MP3Player;
		protected var _albumPreview:AlbumPreviewer;
		protected var _timer:Timer;
		
		public function EMusicTest() 
		{
			_eMusic = EMusicClient.getInstance();
			
			_albumWs = _eMusic.album;
			//_albumWs.addEventListener(AlbumSearchEvent.SUCCESS, doAlbumResults);
			
			_artistWs = _eMusic.artist;	
			//_artistWs.addEventListener(ArtistSearchEvent.SUCCESS, doArtistResults);
			
			_m3uClient = M3UClient.getInstance();
			//_m3uClient.addEventListener(M3UEvent.M3U_RECEIVED, doSampleTracksReady);
			
			_albumPreview = new AlbumPreviewer();
			// _albumPreview.preview("Infected Mushroom", "Converting Vegetarians");
			_albumPreview.preview("Rise Against", "The Unraveling");
			
			_timer = new Timer(9000, 1);
			_timer.addEventListener(TimerEvent.TIMER_COMPLETE, doTimerComplete);
			_timer.start();
			
		}
		
		protected function doTimerComplete(evt:TimerEvent):void
		{
			_albumPreview.preview("Adham Shaikh", "Collectivity");
			//_albumPreview.preview("Infected Mushroom", "Converting Vegetarians");
		}
		
		protected function testArtistSearch():void
		{
			var artistRequest:ArtistSearchRequest = new ArtistSearchRequest();
			artistRequest.term = "Infected Mushroom";
			_artistWs.search(artistRequest);
		}
		
		protected function doAlbumResults(evt:AlbumSearchEvent):void
		{
			trace("count:" + evt.feed.count);
			var albumData:AlbumData = evt.feed.first();
			trace("[" + albumData.id + "]: " + albumData.name + " @ " + albumData.siteUrl);
			
			var loader:Loader = new Loader();
			addChild(loader);
			loader.load(new URLRequest(albumData.image));
			
			_m3uClient.getByUrl(albumData.sample);
		}
		
		protected function doArtistResults(evt:ArtistSearchEvent):void
		{
			trace("count:" + evt.feed.count);
			var artistData:SummaryData = evt.feed.first();
			trace("[" + artistData.id + "]: " + artistData.name + " @ " + artistData.siteUrl);
			trace("total:" + evt.pagination.total + ", pages:" + evt.pagination.pages);
			
			requestAlbumsByArtistId(artistData.id);
		}
		
		protected function requestAlbumsByArtistId(id:Number):void
		{
			var albumRequest:AlbumSearchRequest = new AlbumSearchRequest();
			albumRequest.artistId = id;
			_albumWs.search(albumRequest);
		}
		
		protected function doSampleTracksReady(evt:M3UEvent):void
		{
			var feed:SampleTrackFeed = evt.feed;
			trace("tracks:" + feed.count);
			var track:SampleTrackData = feed.getAt(2);
			trace("[" + track.length + "] " + track.name + " @ " + track.url);
			
			_mp3Player = MP3Player.getInstance();
			_mp3Player.playMedia(new MediaData( -1, track.name, track.length, track.url));
		}
	}
}